import db from './db'
import Mock from 'mockjs'
import jwt from 'jsonwebtoken'

Mock.mock('admin/login', 'post', ({ body }) => {
  const { username, password } = JSON.parse(body)
  // 账号未审核
  const stateResult = db.select('Oppo_admin', item => item.username === username && item.state === 1)
  if (stateResult && stateResult.length > 0) return { err: 103, desc: '您的账号尚未通过审核!' }
  // 登录判断
  const loginResult = db.select('Oppo_admin', item => {
    return item.username === username &&
            item.password === password &&
            item.state === 2
  })
  if (loginResult && loginResult.length > 0) {
    let token = ''
    try {
      token = jwt.sign({ data: username }, '3210', { expiresIn: '5d' })
    } catch (err) {
      console.log(err)
    }
    return { err: 0, token, desc: '登录成功' }
  } else {
    return { err: 100, desc: '用户或者密码错误' }
  }
})
